package top.kaoshanji.v1ch13a.repositories;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import top.kaoshanji.v1ch13a.model.Account;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/1 18:10
 */
@Repository
public class AccountRepository {

    private final JdbcTemplate jdbcTemplate;

    public AccountRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Account findAccountById(long id) {
        String sql = " SELECT * FROM account WHERE id = ? ";
        return jdbcTemplate.queryForObject(sql, new AccountRowMapper(), id);
    }

    public List<Account> findAllAccounts() {
        String sql = " SELECT * FROM account ";
        return jdbcTemplate.query(sql, new AccountRowMapper());
    }

    public void changeAmount(long id, BigDecimal amount) {
        String sql = " UPDATE account SET amount = ? WHERE id = ? ";
        jdbcTemplate.update(sql, amount, id);
    }

}
